﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using KeRui.Common;
using KeRui.Model;

public partial class Manage_Cms_CmsSiteAdminFlag : Page
{
    private readonly string Act = ManageHelper.PageAct();
    private readonly int id = PageRequest.GetInt("id", -1);
    protected readonly List<AdminFlag> listAdminFlag = new List<AdminFlag>();
    private readonly int siteID = PageRequest.GetInt("siteID", -1);
    protected ArrayList list;
    protected CmsSite modelCmsSite;
    protected string sysRole;
    protected string[] sysRoles;

    protected void Page_Load(object sender, EventArgs e)
    {
        KeRui.BLL.CmsSite bllCmsSite = new KeRui.BLL.CmsSite();
        modelCmsSite = bllCmsSite.GetModel(siteID);
        KeRui.BLL.AdminFlag bllAdminFlag = new KeRui.BLL.AdminFlag();
        foreach (AdminFlag model in bllAdminFlag.GetList(0, 1))
        {
            foreach (AdminFlag modelC in bllAdminFlag.GetList(model.ID, (int)ManageEnum.AdminFlagType.Site))
            {
                listAdminFlag.Add(modelC);
            }
        }

        list = (ArrayList)Session["SourcePermission"];

        if (list != null)
        {
            sysRole = string.Empty;
            foreach (WebsitePermissions websitePermission in list)
            {
                if (websitePermission.SiteID == siteID)
                {
                    sysRole = websitePermission.Permissions;
                }
            }
            sysRoles = sysRole.Split(',');
        }

        switch (Act)
        {
            case "action":
                switch (PageRequest.GetString("sub"))
                {
                    case "确 定":
                        SubmitPage();
                        break;
                    case "返 回":
                        Response.Redirect("/Manage/Admin/RoleItem.aspx?id=" + id);
                        Response.End();
                        break;
                }
                break;
        }
    }

    private void SubmitPage()
    {
        KeRui.BLL.AdminFlag bllAdminFlag = new KeRui.BLL.AdminFlag();
        string checkPermissions = bllAdminFlag.GetList(0, (int)ManageEnum.AdminFlagType.Site)[0].Flag + "," +
                                  PageRequest.GetString("websitePermissions");
        WebsitePermissions websitePermissions = new WebsitePermissions(siteID, checkPermissions);
        ArrayList list = (ArrayList)Session["PermissionsKey"];
        if (list != null)
        {
            ArrayList list2 = new ArrayList();
            foreach (WebsitePermissions info in list)
            {
                if (info.SiteID != siteID)
                {
                    list2.Add(info);
                }
            }
            list2.Add(websitePermissions);
            Session["PermissionsKey"] = list2;
        }
        Response.Redirect("/Manage/Admin/RoleItem.aspx?id=" + id + "&act=edit");
        Response.End();
    }
}